Ext.define('Oa.controller.ClientController',{
    extend:'Ext.app.Controller',
    stores:['ClientStore'],
    models:[],
    views:[
        'client.ShowPanel',
        'client.EditPanel'
    ],
    init:function(){
        this.control({
            'clientshow>grid':{
                render:function(){
                    Ext.getCmp('clientGrid').store.load();
                },
                selectionchange:function(model, records,obj){
                     if (records[0]) {
                        Ext.getCmp('update').setDisabled(false);
                        Ext.getCmp('del').setDisabled(false);
                        var form = Ext.getCmp('clientedit').getForm();
                        form.loadRecord(records[0]);
                    }
                }
            },
            'clientedit button[action = update]':{
                click:function(obj){
                    if(obj.text == "修改"){
                        Ext.getCmp('clientGrid').setDisabled(true);
	                    obj.setText("完成");
                        Ext.getCmp("add").setText("取消");
	                    for (var i = 0; i <7; i++) {
	                        obj.up('clientedit').down("#Edit"+i).setDisabled(false);
	                    }
                    }else{
                        //提交
                        var form = obj.up('form').getForm();
                        if (form.isValid()) {
                            obj.setText("修改");
                            Ext.Ajax.request({
	                            url:'../clientAction/addOrUpdateClient.do',
	                            params:form.getValues(true),
	                            success:function(){
	                                Ext.MessageBox.alert("提示","操作成功！");
                                    Ext.getCmp('clientGrid').setDisabled(false);
	                                Ext.getCmp('clientGrid').store.load();
                                    Ext.getCmp('update').setDisabled(true);
			                        Ext.getCmp("clientedit").getForm().reset();
			                        Ext.getCmp("del").setDisabled(true);
	                            },
	                            failure:function(form,action){
	                                Ext.MessageBox.alert("错误！","操作失败<br>服务报告了一项错误，或连接失败")
	                            }
                            })
                            Ext.getCmp("add").setText("新增");
                            for (var i = 0; i <7; i++) {
                                obj.up('clientedit').down("#Edit"+i).setDisabled(true);
                             }
                        }
                    }
                }
            },
            'clientedit button[action = add]':{
                click:function(obj){
                    if (obj.text == "新增") {
                        obj.setText("取消");
                        Ext.getCmp('update').setText('完成');
                        Ext.getCmp('update').setDisabled(false);
                        Ext.getCmp("clientedit").getForm().reset();
                        Ext.getCmp("del").setDisabled(true);
                        for (var i = 0; i <7; i++) {
                            obj.up('clientedit').down("#Edit"+i).setDisabled(false);
                        }
                    } else {
		                    Ext.MessageBox.show({
			                    title:'警告',
			                    msg:'该操作不会保存数据!<br>确定要取消吗？',
			                    buttons:Ext.Msg.YESNO,
			                    icon:Ext.Msg.WARNING,
			                    fn:function(is){
			                        if(is=='yes'){
		                                obj.setText("新增");
			                            Ext.getCmp('update').setText('修改');
			                            Ext.getCmp('update').setDisabled(true);
                                        Ext.getCmp('clientGrid').setDisabled(false);
			                            Ext.getCmp("clientedit").getForm().reset();
				                        for (var i = 0; i <7; i++) {
				                            Ext.getCmp("del").setDisabled(false);
				                            Ext.getCmp("update").setDisabled(true);
				                            obj.up('clientedit').down("#Edit"+i).setDisabled(true);
				                        }
			                        }
		                    }
		                });
                    }
                }
            },
            'clientedit button[action = del]':{
                click:function(obj){
                    Ext.MessageBox.show({
                                title:'警告',
                                msg:'该操作不可逆转!<br>确定要删除吗？',
                                buttons:Ext.Msg.YESNO,
                                icon:Ext.Msg.WARNING,
                                fn:function(is){
                                    if(is=='yes'){
                                        var id = Ext.getCmp('clientid').value;
                                        Ext.Ajax.request({
			                                url:'../clientAction/delClient.do',
			                                params:{"id":id},
			                                success:function(){
			                                    Ext.MessageBox.alert("提示","删除成功！");
                                                obj.setDisabled(true);
                                                Ext.getCmp('update').setDisabled(true);
			                                    Ext.getCmp('clientGrid').store.load();
                                                Ext.getCmp("clientedit").getForm().reset();
                                                for (var i = 0; i <7; i++) {
					                               obj.up('clientedit').down("#Edit"+i).setDisabled(true);
					                            }
			                                },
			                                failure:function(){
			                                    Ext.MessageBox.alert("错误！","操作失败<br>服务报告了一项错误，或连接失败")
			                                }
		                                 })                                   
                                    }
                            }
                        });
                }
            }
        })
    }
})